package com.hksj.Intelligentmanagement.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hksj.Intelligentmanagement.auth.dto.UserDTO;
import com.hksj.Intelligentmanagement.auth.service.UserService;
import com.hksj.Intelligentmanagement.auth.vo.UserVo;
import com.hksj.Intelligentmanagement.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 陈章
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO
 * @createTime 2022年08月18日 09:48
 */

@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/user")
public class UserController {

	@Autowired
	private UserService userService;

	@ApiOperation(value = "用户新增")
	@PostMapping("/save")
	public Result saveUser(@RequestBody @Validated(UserDTO.Save.class) UserDTO userDTO) {
		userService.insert(userDTO);
		return Result.ok();
	}

	@ApiOperation(value = "用户删除")
	@GetMapping("/delUser{id}")
	public Result delUser(@PathVariable("id") String id) {
		userService.del(id);
		return Result.ok();
	}

	@ApiOperation(value = "用户修改")
	@PostMapping("/updateUser")
	public Result updateUser(@RequestBody @Validated(UserDTO.Update.class) UserDTO userDTO) {
		userService.updateUser(userDTO);
		return Result.ok();
	}

	/**
	 * 查询所有用户
	 * @return
	 */
	@ApiOperation(value = "查询所有用户 ")
	@PostMapping("select")
	public Result selectAllUser(
		@ApiParam(value = "账号状态（0正常 1停用）",required = false) @RequestParam(required = false) String status,
		@ApiParam(value = "用户名",required = false) @RequestParam(required = false) String username,
		@ApiParam(value = "起始页",required = false,defaultValue = "1") @RequestParam(required = false,defaultValue = "1") Integer pageNum,
		@ApiParam(value = "每页大小",required = false,defaultValue = "10") @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
		IPage<UserVo> iPage = userService.findAll(status,username,pageNum,pageSize);
		return Result.ok(iPage);
	}
}